import request from '@/utils/request'
const pathMap = {
  userUpdatePwdPath: '/api/user/updatePwd/',
  personInfoPath: '/api/user/personInfo/',
  piupdatePath: '/api/user/updatePersonInfo/',
  queryPath: '/api/user/userGrid/',
  queryWithOutPagePath: '/api/user/userLists',
  deletePath: '/api/user/delete/',
  createPath: '/api/user/create',
  updatePath: '/api/user/update',
  editPasswordPath: '/api/user/adminUpdatePwd',
  editStatusPath: '/api/user/updateStatus',
  unlockPath: '/api/user/unlock/',
  queryAssistPath: '/api/user/assistInfo/',
}
export async function requestUserPage(data) {
  let conditions = []
  if (data.nickName) {
    conditions.push({
      field: `concat(nick_name)`,
      operator: 'like',
      value: data.nickName,
    })
  }
  if (data.deptId) {
    conditions.push({
      field: 'd.dept_id',
      operator: 'in',
      value: [data.deptId],
    })
  }
  if (data.roleIds && data.roleIds.length > 0) {
    conditions.push({
      field: 'r.role_id',
      operator: 'in',
      value: data.roleIds,
    })
  }
  return request({
    url: `${pathMap.queryPath}${data.pageNo}/${data.pageSize}`,
    method: 'post',
    data: {
      pageSize: data.pageSize,
      goPage: data.pageNo,
      conditions,
    },
  })
}

//通过id获取用户信息
export function requestUserNames(data) {
  return request({
    url: `/api/user/userNames`,
    method: 'post',
    data,
  })
}

export function userListsWithOutPage(data) {
  return request({
    url: pathMap.queryWithOutPagePath,
    method: 'post',
    data,
    loading: true,
  })
}

export const requestUserCreate = (data) => {
  return request({
    url: pathMap.createPath,
    method: 'post',
    loading: true,
    data,
  })
}

export const requestUserUpdate = (data) => {
  return request({
    url: pathMap.updatePath,
    method: 'post',
    data,
  })
}

export const requestUserDelete = ({ userId }) => {
  return request({
    url: pathMap.deletePath + userId,
    method: 'post',
  })
}

export const requestUserPasswordReset = (data) => {
  return request({
    url: pathMap.editPasswordPath,
    method: 'post',
    data,
  })
}

export const requestUserStatusUpdate = (data) => {
  return request({
    url: pathMap.editStatusPath,
    method: 'post',
    data,
  })
}

export const personInfo = () => {
  return request({
    url: pathMap.personInfoPath,
    method: 'post',
  })
}

export const updatePerson = (data) => {
  return request({
    url: pathMap.piupdatePath,
    method: 'post',
    data,
  })
}

export const updatePersonPwd = (data) => {
  return request({
    url: pathMap.userUpdatePwdPath,
    method: 'post',
    data,
  })
}

export const allRoles = () => {
  return request({
    url: '/api/user/all',
    method: 'post',
    loading: true,
  })
}

export function roleGrid(pageIndex, pageSize, data) {
  return request({
    url: '/api/user/roleGrid/' + pageIndex + '/' + pageSize,
    method: 'post',
    data,
    loading: true,
  })
}

export function ldapUsers(data) {
  return request({
    url: '/plugin/ldap/users',
    method: 'post',
    loading: true,
  })
}

export function saveLdapUser(data) {
  return request({
    url: '/api/user/sync',
    method: 'post',
    loading: true,
    data,
  })
}

export function existLdapUsers() {
  return request({
    url: '/api/user/existLdapUsers',
    method: 'post',
    loading: false,
  })
}

export function unLock(username) {
  return request({
    url: pathMap.unlockPath + username,
    method: 'post',
    loading: false,
  })
}

export function queryAssist(userId) {
  return request({
    url: pathMap.queryAssistPath + userId,
    method: 'post',
    loading: false,
  })
}

export function requestUserKeyGet() {
  return request({
    url: 'user/key/get',
    method: 'get',
    loading: false,
  })
}

export function getMctToken(params) {
  return request({
    url: '/api/iam/getMctToken',
    method: 'get',
    loading: false,
    params,
  })
}
